<template>
    <div class="col-12">
        <div class="row">
            <div class="col-sm-6">
                <div class="card border-0 shadow-sm">
                    <img class="cover-img card-img-top" :src="playlist.coverImgUrl" :alt="playlist.name">
                    <div class="card-body">
                        <h4 class="card-title mb-0">{{ playlist.name }}</h4>
                        <p class="text-muted mt-3 mb-0">{{ playlist.description }}</p>
                    </div>
                    <div class="card-footer">
                        <small class="mr-1" v-for="tag in playlist.tags" :key="tag">#{{ tag }}</small>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <ul class="list-group border-0 shadow-sm">
                    <li class="list-group-item list-group-item-action border-0" v-for="track in playlist.tracks" :key="track.id" @click="playTrack(track.id)">
                        {{ track.name }}
                        <div class="ml-auto text-muted" v-for="artist in track.artists" :key="artist.id">
                            {{ artist.name }}
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "playlist",
        data() {
            return {
                playlist: {
                    name: "",
                    coverImgUrl: "",
                    tracks: [],
                    description: "",
                    tags: []
                }
            }
        },
        beforeMount() {
            this.getPlaylistData()
        },
        methods: {
            getPlaylistData() {
                this.$http.get(`/api/playlist/detail?id=${this.$route.params.listId}`).then(response => {
                    this.playlist.name = response.data.result.name
                    this.playlist.coverImgUrl = response.data.result.coverImgUrl
                    this.playlist.tracks = response.data.result.tracks
                    this.playlist.description = response.data.result.description
                    this.playlist.tags = response.data.result.tags
                })
            },
            playTrack(id) {
                this.$store.dispatch("setPlayTrackAction", { id: id })
            }
        }
    }
</script>